﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMSmanagement.IBaseService;
using WMSmanagement.IBaseService.Sale;
using WMSmanagement.IBaseService.Sale.DTO;
using WMSmanagement.Model;

namespace WMSmanagement.BaseService
{
    [ApiExplorerSettings(GroupName = "刘海通")]

    public class SaleService:ApplicationService,ISaleService
    {
        private readonly IRepository<Sales,int> _repository;
        private readonly IRepository<SaleDetils, int> _SaleDetilsrepository;
        private readonly IRepository<Products, int> _Productsrepository;

        public SaleService(IRepository<Sales, int> repository, IRepository<SaleDetils, int> saleDetilsrepository, IRepository<Products, int> productsrepository)
        {
            _repository = repository;
            _SaleDetilsrepository = saleDetilsrepository;
            _Productsrepository = productsrepository;
        }
        /// <summary>
        /// 显示销售管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<GetSaleDTO> GetSaleAsync(RequestSaleDTO model)
        {
            var list = from A in await _repository.GetListAsync()
                       join
                       B in await _SaleDetilsrepository.GetListAsync() on
                       A.SalesOrderCode equals B.SalesOrderCode
                       join
                       C in await _Productsrepository.GetListAsync() on
                       B.ProductCode equals C.ProductCode
                       select new ShowSaleDTO { 
                           Id=A.Id,
                           SalesOrderCode=A.SalesOrderCode,
                           OutQuantity=A.OutQuantity,
                           ReturnQuantity = A.ReturnQuantity,
                           ReturnOutQuantity = A.ReturnOutQuantity,
                           CreateTime = A.CreateTime,
                           Maker = A.Maker,
                           MakeTime = A.MakeTime,
                           Remark = A.Remark,
                           DeliveryDate = A.DeliveryDate,
                           CustomerCode = A.CustomerCode,
                           CustomerName = A.CustomerName,
                           CustomerContact=A.CustomerContact,
                           CustomerPhone = A.CustomerPhone,
                           ProductCode=C.ProductCode,
                           ProductName=C.ProductName,
                           ProductSpecification=C.ProductSpecification,
                           Category=C.Category,
                           StorageUnit = C.StorageUnit,
                           Unit = C.Unit,
                           UnitPrice = C.UnitPrice,
                           Quantity=B.Quantity,
                       };
            list=list.WhereIf(!string.IsNullOrEmpty(model.SalesOrderCode),c=>c.SalesOrderCode.Contains(model.SalesOrderCode));
            list=list.WhereIf(!string.IsNullOrEmpty(model.ProductCode),c=>c.ProductCode.Contains(model.ProductCode));
            list=list.WhereIf(!string.IsNullOrEmpty(model.ProductName),c=>c.ProductName.Contains(model.ProductName));
            var listcount=list.Count();
            return new GetSaleDTO
            {
                list=list.ToList(),
                listcount=listcount,
            };
        }
        /// <summary>
        /// 添加销售管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddSaleAsync(AddSaleDTO model)
        {
            

            var saleList = model.MapTo<Sales>();
            saleList.OutQuantity = 0;
            saleList.ReturnQuantity = " ";
            saleList.ReturnOutQuantity = 0;
            saleList.CreateTime = DateTime.Now;
            await _repository.InsertAsync(saleList);
            var saleDtiels = new List<SaleDetils>();

            foreach (var item in model.Ids) 
            {
                var saleDtiel = new SaleDetils()
                {
                    ProductCode=item.Id,
                    SalesOrderCode=model.SalesOrderCode,
                    Quantity=item.Num,
                };
                saleDtiels.Add(saleDtiel);
            }
             await _SaleDetilsrepository.InsertManyAsync(saleDtiels);

            return true;
        }

        /// <summary>
        /// 查询商品表
        /// </summary>
        /// <returns></returns>
        public  async Task<List<ProductsDTO>> GetProductsAsync()
        {
            var list=await _Productsrepository.GetListAsync();
            var result = list.MapToList<ProductsDTO>();
            return result;
        }
        /// <summary>
        /// 修改销售管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSaleAsync(UpdateSaleDTO model)
        {


            var saleList = model.MapTo<Sales>();
            await _repository.UpdateAsync(saleList);
            var detielList =await _SaleDetilsrepository.GetListAsync(c => c.SalesOrderCode == model.SalesOrderCode);
           await _SaleDetilsrepository.DeleteManyAsync(detielList);
            var saleDtiels = new List<SaleDetils>();

            foreach (var item in model.Ids)
            {
                var saleDtiel = new SaleDetils()
                {
                    ProductCode = item.Id,
                    SalesOrderCode = model.SalesOrderCode,
                    Quantity = item.Num,
                };
                saleDtiels.Add(saleDtiel);
            }
             await _SaleDetilsrepository.InsertManyAsync(saleDtiels);

            return true;
        }
        /// <summary>
        /// 删除销售管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteSaleAsync(string id)
        {
            var saleDetil =await _SaleDetilsrepository.FindAsync(c => c.ProductCode == id);
            await _SaleDetilsrepository.DeleteAsync(saleDetil);
            var count=await _SaleDetilsrepository.CountAsync(c=>c.SalesOrderCode==saleDetil.SalesOrderCode);
            if(count==0)
            {
               await _repository.DeleteAsync(c=>c.SalesOrderCode==saleDetil.SalesOrderCode);
            }
            //var list = await _SaleDetilsrepository.GetListAsync(c => c.SalesOrderCode == id);
            //await _SaleDetilsrepository.DeleteManyAsync(list);
            //await _repository.DeleteAsync(c=>c.SalesOrderCode==id);
            return true;
        }
        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> ReturnSaleAsync(int id)
        {
            var list=await _repository.FindAsync(id);
            list.ReturnQuantity = "是";
            var result=await _repository.UpdateAsync(list)!=null;
            return result;
        }

    }
}
